//
// Created by root on 2022/3/17.
//

#include <stdio.h>
#include <ctype.h>
#include "../atof/atof.h"
#include "./operator.h"

// 读取字符的缓冲区
static char buffer = '\0';


int gettop(char line[]) {

    if (isOperator(buffer)) {
        line[0] = buffer;
        line[1] = '\0';
        buffer = '\0';
        return 1;
    }

    int length = 0;
    int i = 0;
    char c;
    while ((c = getchar()) != EOF && c != '\n' && c != ' ') {
        i++;
        if (isdigit(c)) {
            line[length++] = c;
        } else if (isOperator(c)) {
            break;
        } else {
            // 其他字符都忽略
        }
    }

    // 因为多读了一个字符，保存在这里
    if (length == 0 && isOperator(c)) {
        line[length++] = c;
    } else {
        buffer = c;
    }

    line[length] = '\n';
    line[length + 1] = '\0';
    return length;
}
